package tech.horgee.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.horgee.common.MyCode;
import tech.horgee.common.MyResult;
import tech.horgee.config.AppConfig;
import tech.horgee.entity.Attendance;
import tech.horgee.group.InsertGroup;
import tech.horgee.group.UpdateGroup;
import tech.horgee.service.AttendanceService;
import tech.horgee.utils.FileUploadUtils;
import tech.horgee.utils.MimeTypeUtils;

import java.io.IOException;
import java.util.Set;

/**
 * 考勤表接口
 *
 * @author haojun
 * @since 2023-12-13
 */
@Slf4j
@RestController
@RequestMapping("/attendance")
@RequiredArgsConstructor
public class AttendanceController {
    private final AttendanceService service;

    /**
     * 分页查询列表
     *
     * @param index  当前页|1
     * @param size   页大小|10
     * @param entity 查询实体
     * @return 所有数据
     */
//    @SaCheckPermission("attendance:query")
    @PostMapping("page/{index}/{size}")
    public MyResult selectAll(@RequestBody(required = false) Attendance entity, @PathVariable Long index, @PathVariable Long size) {
        Page<Attendance> page = new Page<>(index, size);
        return MyResult.of(service.page(page, entity));
    }

    /**
     * 不分页查询列表
     *
     * @param entity 实体对象
     * @return 查询结果
     */
//    @SaCheckPermission("attendance:query")
    @PostMapping("list")
    public MyResult list(@RequestBody(required = false) Attendance entity) {
        return MyResult.of(service.list(entity));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
//    @SaCheckPermission("attendance:query")
    @GetMapping("{id}")
    public MyResult getById(@PathVariable Long id) {
        return MyResult.of(service.getById(id));
    }

    /**
     * 新增单条数据
     *
     * @param entity 实体对象
     * @return 新增结果
     */
//    @SaCheckPermission("attendance:add")
    @PostMapping
    public MyResult save(@RequestBody @Validated(InsertGroup.class) Attendance entity) {
        service.save(entity);
        return MyResult.success();
    }

    /**
     * 修改单条数据
     *
     * @param entity 实体对象
     * @return 修改结果
     */
//    @SaCheckPermission("attendance:edit")
    @PutMapping
    public MyResult update(@RequestBody @Validated(UpdateGroup.class) Attendance entity) {
        service.update(entity);
        return MyResult.success();
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键集合
     * @return 操作结果
     */
//    @SaCheckPermission("attendance:delete")
    @PostMapping("delete")
    public MyResult deleteByIds(@RequestBody Set<Long> ids) {
        service.deleteByIds(ids);
        return MyResult.success();
    }

    /**
     * 查询今日打卡记录
     */
    @GetMapping("get-sign-time")
    public MyResult getSignTime() {
        return MyResult.of(service.getSignTime());
    }

    /**
     * 打卡
     */
    @PostMapping("sign-in")
    public MyResult signIn(@RequestBody Attendance dto) {
        service.signIn(dto);
        return MyResult.success();
    }

    /**
     * 考勤异常处理
     */
    @PostMapping("handle")
    public MyResult handleException(@RequestBody Attendance dto) {
        service.handleException(dto);
        return MyResult.success();
    }

    /**
     * 上传附件
     *
     * @param file 附件
     */
    @PostMapping("uploadAnnex")
    public MyResult uploadAnnex(@RequestParam("annex") MultipartFile file) throws IOException {
        if (file.isEmpty()) return MyCode.DATA_VERIFY_ERROR.toResult("图片上传失败");
        String path = FileUploadUtils.upload(AppConfig.getAttendPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
        return MyResult.of(path);
    }
}
